package com.wy.component.qrcode;
import java.awt.Color;
import java.io.File;
import java.io.OutputStream;
import java.net.URISyntaxException;

import com.google.zxing.common.BitMatrix;

//生成二维码
public class GenerateQRcodeUtil {
	
   @SuppressWarnings({ "unchecked", "rawtypes" })
	public static String createImage(String encryptData,String fileName,String path) throws URISyntaxException{
	   //获取根路径
	   String rootPath = GenerateQRcodeUtil.class.getClassLoader().getResource("/").toURI().getPath();
//	   String currentath=aa+""+path;
		//logo路径
		String logoPath=rootPath.replace("WEB-INF/classes/", "")+"resources/imgCommon/qrcodeIcon.png";
		int width =300;
		int height =300;
//		File dir=new File(currentath+"/"+path);
		File dir=new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		File f=new File(dir, ".png");
//		String imagePath=currentath+"/"+fileName+".png";
		String imagePath=path+"/"+fileName+".png";
		if(!f.exists()){
			BitMatrix matrix = MatrixToImageWriterEx.createQRCode(encryptData, width, height);
			MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4);
			try { 
				MatrixToImageWriterEx.writeToFile(matrix, "png", imagePath, logoPath, logoConfig);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return fileName+".png";
	}
   
   //报名成功，生成电子票二维码
   public static String createUserQRCodeImage(String encryptData, String userAccount) throws URISyntaxException{
	   //获取根路径
	   String rootPath = GenerateQRcodeUtil.class.getClassLoader().getResource("/").toURI().getPath();
//	   String currentath=aa+""+path;
	   //logo路径
	   String logoPath=rootPath.replace("WEB-INF/classes/", "")+"resources/imgCommon/qrcodeIcon.png";
	   int width =300;
	   int height =300;
//		File dir=new File(currentath+"/"+path);
	   String path =rootPath.replace("WEB-INF/classes/", "")+ "/eticket"; // 真实路径
	   File dir=new File(path);
	   if(!dir.exists()){
		   dir.mkdirs();
	   }
	   File f=new File(dir, ".png");
//		String imagePath=currentath+"/"+fileName+".png";
	   String imagePath=path+"/"+userAccount+".png";
	   if(!f.exists()){
		   BitMatrix matrix = MatrixToImageWriterEx.createQRCode(encryptData, width, height);
		   MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4);
		   try { 
			   MatrixToImageWriterEx.writeToFile(matrix, "png", imagePath, logoPath, logoConfig);
		   } catch (Exception e) {
			   e.printStackTrace();
		   }
	   }
	   return userAccount+".png";
   }
   
   
   
   @SuppressWarnings({ "unchecked", "rawtypes" })
   public static OutputStream createImageStream(String encryptData,String fileName,String path) throws URISyntaxException{
	   //获取根路径
	   String aa=GenerateQRcodeUtil.class.getClassLoader().getResource("/").toURI().getPath();
//	   String currentath=aa+""+path;
	   //logo路径
	   String logoPath=aa+"meetings/view.jpg";
	   int width =300;
	   int height =300;
//		File dir=new File(currentath+"/"+path);
	   File dir=new File(path);
	   if(!dir.exists()){
		   dir.mkdirs();
	   }
	   File f=new File(dir, ".png");
//		String imagePath=currentath+"/"+fileName+".png";
	   String imagePath=path+"/"+fileName+".png";
	   OutputStream os = null;
	   if(!f.exists()){
		   BitMatrix matrix = MatrixToImageWriterEx.createQRCode(encryptData, width, height);
		   MatrixToLogoImageConfig logoConfig = new MatrixToLogoImageConfig(Color.BLUE, 4);
		   try { 
			   os = MatrixToImageWriterEx.writeToStream(matrix, "png", imagePath, logoPath, logoConfig);
		   } catch (Exception e) {
			   e.printStackTrace();
		   }
	   }
	   return os;
   }
}
